
#if defined vcolors_inc
 #endscript
#endif
#define vcolors_inc

#if !defined PATH_VCOLORS
 #define PATH_VCOLORS "data/carcolors.dat"
#endif

#define MAX_VCOLORS 5



static vcolors_arr[212][MAX_VCOLORS];


vcolors_init()
{
	/*for(new i = 0; i < 212; i++)
		for(new j = 0; j < MAX_VCOLORS; j++)
			vcolors_arr[i][j] = -1;
	*/
	Load_VCOL(PATH_VCOLORS, vcolors_arr);
}


stock GetVehicleModelColor1(modelid, idx)
{
	/*
	return vcolors_arr[modelid-400][idx] & 0xFFFF;
	*/
	return getbyte(vcolors_arr[modelid-400], idx * 2);
}

stock GetVehicleModelColor2(modelid, idx)
{
	/*
	return (vcolors_arr[modelid-400][idx] >> 16) & 0xFFFF;
	*/
	return getbyte(vcolors_arr[modelid-400], idx * 2 + 1);
}

stock GetVehicleModelRandomColor(modelid, &col1, &col2)
{
	new am = GetVehicleModelColorsCount(modelid);
	if(am)
	{
		am = random(am / 2);
		col1 = GetVehicleModelColor1(modelid, am );
		col2 = GetVehicleModelColor2(modelid, am );
		return true;
	}
	else
	{
		col1 = -1;
		col2 = -1;
		return false;
	}
}

/*
stock GetVehicleModelRandomColor1(modelid)
{
	new const am = GetVehicleModelColorsCount(modelid);
	new rcol = random(am);
	//return am?vcolors_arr[modelid-400][random(am)] & 0xFFFF:(-1);
	return am?getbyte(vcolors_arr[modelid-400], rcol - rcol%2):(-1);
}

stock GetVehicleModelRandomColor1(modelid)
{
	new const am = GetVehicleModelColorsCount(modelid);
	new rcol = random(am);
	//return am?vcolors_arr[modelid-400][random(am)] & 0xFFFF:(-1);
	return am?getbyte(vcolors_arr[modelid-400], rcol - rcol%2):(-1);
}
*/


stock GetVehicleModelColorsCount(modelid)
{
/*
	new i = 0;
	new const modelidx = modelid - 400;
	while(i < MAX_VCOLORS && vcolors_arr[modelidx][i])
		i++;
	return 4 * i;*/

	return getbyte(vcolors_arr[modelid-400], MAX_VCOLORS*4-1);
}

static token_by_delim(const string[], return_str[], delim, start_index)
{
	new x=0;
	while(string[start_index] == ' ' || string[start_index] == '\t')
		start_index++;
	while(string[start_index] != EOS && string[start_index] != delim && string[start_index] != '#')
	{
		return_str[x] = string[start_index];
		x++;
		start_index++;
	}
	return_str[x] = EOS;
	if(string[start_index] == EOS) start_index = (-1);
	return start_index;
}


static checkline(const line[])
{
	if(line[0] == '#' || line[0] == '\r') return false;
	if(line[0] == EOF || line[0] == EOS) return false;
	return true;
}

static Load_VCOL(const filename[], dest[][])
{
	new File:file_ptr;
	new line[256];
	new var_from_line[64];

	new i;
	new modelid;


	new index;

	file_ptr = fopen(filename,filemode:io_read);
	if(!file_ptr)
	{
		printf("Unable to read file %s", filename);
		return 0;
	}


	while(fread(file_ptr,line,sizeof(line)) > 0)
	{
		if(!checkline(line)) continue;

		index = 0;

		index = token_by_delim(line,var_from_line,':',index);
		if(index == (-1)) continue;
		modelid = strval(var_from_line) - 400;
		if(modelid < 0 || modelid > 211) continue;

		index++;
		i = 0;
		while(index != -1)
		{
			if(i >= MAX_VCOLORS * 4 - 1) break; // overflow
			index = token_by_delim(line,var_from_line,',',index+1);
			if(index == (-1)) break;

			//dest[modelid][i] = strval(var_from_line);
			setbyte(dest[modelid], i, strval(var_from_line));

/*			index = token_by_delim(line,var_from_line,',',index+1);
			if(index == (-1)) break;
			dest[modelid][i] |= strval(var_from_line) << 16;
*/
			i++;
		}

		setbyte(dest[modelid], MAX_VCOLORS * 4 - 1, i);

		//vcolors_arr[modelid][MAX_VCOLORS-1] = (i > MAX_VCOLORS - 2) ? MAX_VCOLORS-1 : i;

	}

	fclose(file_ptr);

	return true;
}

#if !defined _bitmask
	#define _bitmask(%0) (((1)<<(%0))-(1))

	#define getbits(%0,%1,%2) (((%0)>>(%1))&_bitmask(%2))
	#define setbits(%0,%1,%2,%3) ((~(((~(%3))&_bitmask(%2))<<(%1)))&(((%3)<<(%1))|(%0)))
#endif




static stock setbyte(arr[], idx, val)
{
	return arr[idx / 4] = setbits(arr[idx / 4], (idx % 4) * 8, 8, val);
}

static stock getbyte(arr[], idx)
{
	return getbits(arr[idx / 4], (idx % 4) * 8, 8);
}


